package com.ruoyi.business.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.ruoyi.business.domain.EquipmentCollectParameter;
import com.ruoyi.business.service.IEquipmentCollectParameterService;
import com.ruoyi.business.vo.EquipmentCollectParameterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.EquipmentinfoMapper;
import com.ruoyi.business.domain.Equipmentinfo;
import com.ruoyi.business.service.IEquipmentinfoService;

/**
 * 主设备台账Service业务层处理
 *
 * @author DL
 * @date 2025-07-01
 */
@Service
public class EquipmentinfoServiceImpl implements IEquipmentinfoService {
    @Autowired
    private EquipmentinfoMapper equipmentinfoMapper;
    @Autowired
    private IEquipmentCollectParameterService equipmentCollectParameterService;


    /**
     * 查询主设备台账
     *
     * @param ID 主设备台账主键
     * @return 主设备台账
     */
    @Override
    public Equipmentinfo selectEquipmentinfoByID(String ID) {
        return equipmentinfoMapper.selectEquipmentinfoByID(ID);
    }

    /**
     * 查询主设备台账列表
     *
     * @param equipmentinfo 主设备台账
     * @return 主设备台账
     */
    @Override
    public List<Equipmentinfo> selectEquipmentinfoList(Equipmentinfo equipmentinfo) {
        return equipmentinfoMapper.selectEquipmentinfoList(equipmentinfo);
    }

    /**
     * 新增主设备台账
     *
     * @param equipmentinfo 主设备台账
     * @return 结果
     */
    @Override
    public int insertEquipmentinfo(Equipmentinfo equipmentinfo) {
        return equipmentinfoMapper.insertEquipmentinfo(equipmentinfo);
    }

    /**
     * 修改主设备台账
     *
     * @param equipmentinfo 主设备台账
     * @return 结果
     */
    @Override
    public int updateEquipmentinfo(Equipmentinfo equipmentinfo) {
        return equipmentinfoMapper.updateEquipmentinfo(equipmentinfo);
    }

    /**
     * 批量删除主设备台账
     *
     * @param IDs 需要删除的主设备台账主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentinfoByIDs(String[] IDs) {
        return equipmentinfoMapper.deleteEquipmentinfoByIDs(IDs);
    }

    /**
     * 删除主设备台账信息
     *
     * @param ID 主设备台账主键
     * @return 结果
     */
    @Override
    public int deleteEquipmentinfoByID(String ID) {
        return equipmentinfoMapper.deleteEquipmentinfoByID(ID);
    }

    /**
     * 查询主设备台账的厂商列表
     *
     * @return 厂商列表
     */
    @Override
    public List<Equipmentinfo> getMakerList() {
        return equipmentinfoMapper.getMakerList();
    }

    /**
     * 根据厂商查询主设备台账详细信息
     *
     * @param code 厂商
     * @return 主设备台账详情
     */
    @Override
    public Equipmentinfo selectEquipmentinfoByMaker(String code) {
        return equipmentinfoMapper.selectEquipmentinfoByMaker(code);
    }

    /**
     * @author: xienian
     * @description:查询所有设备信息
     * @param:
     * @return: java.lang.String
     * @date: 2025/7/3 10:26
     */
    @Override
    public Map<String, String> queryDeviceDataList() {
        List<Equipmentinfo> equipmentinfos = equipmentinfoMapper.selectEquipmentinfoList(null);

        //查询采集参数信息
        Map<String, String> resultMap = new HashMap<>();
        List<EquipmentCollectParameterVo> equipmentCollectParameters = equipmentCollectParameterService.selectCollectParameterListAndSon();
        Map<String, List<EquipmentCollectParameterVo>> collect = equipmentCollectParameters.stream().collect(Collectors.groupingBy(EquipmentCollectParameterVo::getEquipmentID));
        for (Equipmentinfo equipmentinfo : equipmentinfos) {
            List<EquipmentCollectParameterVo> equipmentCollectParameterVos = collect.get(equipmentinfo.getID());
            Map<String, String> sonMap = new HashMap<>();
            if (CollectionUtil.isNotEmpty(equipmentCollectParameterVos)) {
                sonMap = equipmentCollectParameterVos.stream().collect(HashMap::new, (map, vo) -> map.put(vo.getCollectCode(), vo.getCollectValue()), HashMap::putAll);
            }
            resultMap.put(equipmentinfo.getEquipmentSN(), JSON.toJSONString(sonMap));

        }
        return resultMap;
    }
}
